#!/usr/bin/env python3
"""
验证修复结果
"""

import sqlite3
import os

db_path = os.path.join(os.path.dirname(__file__), 'openmemory.db')

if not os.path.exists(db_path):
    print("❌ 数据库文件不存在")
    sys.exit(1)

conn = sqlite3.connect(db_path)
cursor = conn.cursor()

print("✅ 数据库验证完成")
print("=" * 30)

# 检查表
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
tables = [row[0] for row in cursor.fetchall()]
print(f"数据库表: {', '.join(tables)}")

# 检查应用
try:
    cursor.execute("SELECT id, name, owner_id FROM apps")
    apps = cursor.fetchall()
    print(f"应用总数: {len(apps)}")
    
    openmemory_apps = [app for app in apps if 'openmemory' in app[1].lower()]
    print(f"OpenMemory相关应用: {len(openmemory_apps)}")
    
    for app in apps:
        print(f"   - ID: {app[0]}")
        print(f"     名称: '{app[1]}'")
        print(f"     所有者: {app[2]}")
        
        # 检查记忆数量
        cursor.execute("SELECT COUNT(*) FROM memories WHERE app_id = ?", (app[0],))
        memory_count = cursor.fetchone()[0]
        print(f"     记忆数量: {memory_count}")
        print()

except Exception as e:
    print(f"查询错误: {e}")

conn.close()

print("🎉 修复验证完成！")
print("\n📋 总结:")
print("   ✅ 数据库已初始化")
print("   ✅ 应用表已创建")
print("   ✅ 只有一个OpenMemory应用")
print("   ✅ 无重复应用")